# Windows 构建后自动打包测试脚本
# 用于验证 xray.exe 是否正确打包

Write-Host "═══════════════════════════════════════" -ForegroundColor Cyan
Write-Host "   Flutter VPN 构建验证工具" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""

# 测试1: 检查源文件
Write-Host "测试 1/5: 检查源文件" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────" -ForegroundColor Gray

$sourceFile = "xray-core\xray.exe"
if (Test-Path $sourceFile) {
    $size = (Get-Item $sourceFile).Length
    Write-Host "✅ 源文件存在: $sourceFile" -ForegroundColor Green
    Write-Host "   大小: $([math]::Round($size/1MB, 2)) MB" -ForegroundColor Gray
} else {
    Write-Host "❌ 源文件不存在: $sourceFile" -ForegroundColor Red
    Write-Host "   请确保已下载 xray.exe 到 xray-core 目录" -ForegroundColor Yellow
    exit 1
}
Write-Host ""

# 测试2: 检查 pubspec.yaml 配置
Write-Host "测试 2/5: 检查 pubspec.yaml 配置" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────" -ForegroundColor Gray

$pubspecContent = Get-Content "pubspec.yaml" -Raw
if ($pubspecContent -match "xray-core/xray.exe") {
    Write-Host "✅ pubspec.yaml 包含 xray-core/xray.exe" -ForegroundColor Green
} else {
    Write-Host "❌ pubspec.yaml 未配置 xray-core/xray.exe" -ForegroundColor Red
    Write-Host "   请在 flutter -> assets 中添加: - xray-core/xray.exe" -ForegroundColor Yellow
    exit 1
}
Write-Host ""

# 测试3: 检查构建产物
Write-Host "测试 3/5: 检查构建产物" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────" -ForegroundColor Gray

$buildPath = "build\windows\x64\runner\Release"
if (Test-Path $buildPath) {
    Write-Host "✅ 构建目录存在: $buildPath" -ForegroundColor Green
    
    # 检查主程序
    $exePath = "$buildPath\flutter_vpn.exe"
    if (Test-Path $exePath) {
        $exeSize = (Get-Item $exePath).Length
        Write-Host "✅ 主程序存在: flutter_vpn.exe ($([math]::Round($exeSize/1KB, 2)) KB)" -ForegroundColor Green
    } else {
        Write-Host "⚠️ 主程序不存在，可能需要重新构建" -ForegroundColor Yellow
    }
    
    # 检查 flutter_assets
    $assetsPath = "$buildPath\data\flutter_assets"
    if (Test-Path $assetsPath) {
        Write-Host "✅ flutter_assets 目录存在" -ForegroundColor Green
    } else {
        Write-Host "❌ flutter_assets 目录不存在" -ForegroundColor Red
    }
} else {
    Write-Host "⚠️ 构建目录不存在，需要运行 flutter build windows" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "是否现在执行构建？(Y/N)" -ForegroundColor Cyan
    $response = Read-Host
    if ($response -eq "Y" -or $response -eq "y") {
        Write-Host "正在清理旧构建..." -ForegroundColor Gray
        flutter clean
        Write-Host "正在构建 Windows 版本..." -ForegroundColor Gray
        flutter build windows --release
    } else {
        exit 0
    }
}
Write-Host ""

# 测试4: 检查 xray.exe 是否打包
Write-Host "测试 4/5: 检查 xray.exe 打包情况" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────" -ForegroundColor Gray

$packedXrayPath = "$buildPath\data\flutter_assets\xray-core\xray.exe"
if (Test-Path $packedXrayPath) {
    $packedSize = (Get-Item $packedXrayPath).Length
    $sourceSize = (Get-Item $sourceFile).Length
    
    Write-Host "✅ xray.exe 已打包到 flutter_assets" -ForegroundColor Green
    Write-Host "   路径: $packedXrayPath" -ForegroundColor Gray
    Write-Host "   打包大小: $([math]::Round($packedSize/1MB, 2)) MB" -ForegroundColor Gray
    Write-Host "   源文件大小: $([math]::Round($sourceSize/1MB, 2)) MB" -ForegroundColor Gray
    
    if ($packedSize -eq $sourceSize) {
        Write-Host "✅ 文件大小一致，完整性验证通过" -ForegroundColor Green
    } else {
        Write-Host "⚠️ 文件大小不一致，可能损坏" -ForegroundColor Yellow
        Write-Host "   差异: $($sourceSize - $packedSize) 字节" -ForegroundColor Yellow
    }
} else {
    Write-Host "❌ xray.exe 未打包到 flutter_assets" -ForegroundColor Red
    Write-Host "   这通常是因为:" -ForegroundColor Yellow
    Write-Host "   1. pubspec.yaml 配置不正确" -ForegroundColor Yellow
    Write-Host "   2. 构建前源文件不存在" -ForegroundColor Yellow
    Write-Host "   3. 需要重新运行 flutter build windows" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "建议操作:" -ForegroundColor Cyan
    Write-Host "1. 确保 xray-core\xray.exe 存在" -ForegroundColor Gray
    Write-Host "2. 确保 pubspec.yaml 配置正确" -ForegroundColor Gray
    Write-Host "3. 运行: flutter clean" -ForegroundColor Gray
    Write-Host "4. 运行: flutter build windows --release" -ForegroundColor Gray
}
Write-Host ""

# 测试5: 检查 AssetManifest.json
Write-Host "测试 5/5: 检查 AssetManifest.json" -ForegroundColor Yellow
Write-Host "─────────────────────────────────────" -ForegroundColor Gray

$manifestPath = "$buildPath\data\flutter_assets\AssetManifest.json"
if (Test-Path $manifestPath) {
    $manifestContent = Get-Content $manifestPath -Raw
    if ($manifestContent -match "xray-core/xray.exe") {
        Write-Host "✅ AssetManifest.json 包含 xray-core/xray.exe" -ForegroundColor Green
        
        # 美化输出
        $manifest = $manifestContent | ConvertFrom-Json
        if ($manifest.'xray-core/xray.exe') {
            Write-Host "   清单条目: xray-core/xray.exe" -ForegroundColor Gray
        }
    } else {
        Write-Host "❌ AssetManifest.json 不包含 xray-core/xray.exe" -ForegroundColor Red
    }
} else {
    Write-Host "⚠️ AssetManifest.json 不存在" -ForegroundColor Yellow
}
Write-Host ""

# 总结
Write-Host "═══════════════════════════════════════" -ForegroundColor Cyan
Write-Host "   测试完成" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""

if ((Test-Path $packedXrayPath) -and ((Get-Item $packedXrayPath).Length -eq (Get-Item $sourceFile).Length)) {
    Write-Host "🎉 所有测试通过！" -ForegroundColor Green
    Write-Host ""
    Write-Host "✅ xray.exe 已成功打包" -ForegroundColor Green
    Write-Host "✅ 文件完整性验证通过" -ForegroundColor Green
    Write-Host "✅ 可以进行分发" -ForegroundColor Green
    Write-Host ""
    Write-Host "分发目录: $buildPath" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "建议测试:" -ForegroundColor Yellow
    Write-Host "1. 将 $buildPath 整个目录复制到另一台电脑" -ForegroundColor Gray
    Write-Host "2. 运行 flutter_vpn.exe" -ForegroundColor Gray
    Write-Host "3. 添加服务器配置" -ForegroundColor Gray
    Write-Host "4. 点击连接，验证自动提取和代理功能" -ForegroundColor Gray
    Write-Host ""
} else {
    Write-Host "⚠️ 部分测试未通过" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "请检查上述错误信息并修复" -ForegroundColor Yellow
    Write-Host ""
}

Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
